<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/1/11 0011
 * Time: 下午 5:41
 */
namespace Admin\Model;
class AuthRuleModel{

    public function show_auth_rule(){
        $sql="select * from ".C('DB_PREFIX')."auth_rule ";

        $count=count(M()->query($sql));
        $Page = new \Think\Page($count,C('BACK_PAGE_NUM'));
        $show  = $Page->show();// 分页显示输出

        $sql.=' order by id desc LIMIT '.$Page->firstRow.','.$Page->listRows;

        $list=M()->query($sql);

        return array(
            'empty'=>'<tr><td colspan="20">~~暂无数据</td></tr>',
            'list'=>$list,
            'page'=>$show
        );

    }


    public function add_auth_rule($data){

        if(empty($data['title'])){
            $error="规则名不能为空！！";
        }elseif(M('AuthGroup')->getByTitle(trim($data['title']))){
            $error="规则名已经存在！！";
        }
        if(empty($data['name'])){
            $error="规则唯一标识不能为空！！";
        }elseif(M('AuthGroup')->getByTitle(trim($data['title']))){
            $error="规则唯一标识已经存在！！";
        }



        if($error){
            return array(
                'status'=>'back',
                'message'=>$error
            );
        }
        if(M('AuthRule')->add($data)){

            return array(
                'status'=>'success',
                'message'=>'新增成功',
                'jump'=>U('AuthRule/index')
            );
        }else{
            return array(
                'status'=>'back',
                'message'=>'新增失败'

            );
        }
    }

    public function edit_auth_rule($data){
        $r=M('Admin')->where(array('id'=>$data['id']))->save($data);
        if($r){
            return array(
                'status'=>'success',
                'message'=>'修改成功',
                'jump'=>U('AuthGroup/index')
            );
        }else{
            return array(
                'status'=>'fail',
                'message'=>'修改失败',
                'jump'=>U('AuthGroup/index')
            );
        }
    }

}